﻿using Lee.Cache; 
using Microsoft.AspNetCore.Mvc; 
using WebA.Admin.Contracts; 
using WebApiA.Attributes;
using WebApiA.Code;

namespace WebApiA.Controllers
{
    [ApiController]

    [Route("api/[Controller]/[Action]")]
    public class TestController : ControllerBase
    { 
        private readonly ITestContract _testContract;
        private readonly ITokenManager _tokenManager;
        
        public TestController(ITestContract testContract, ITokenManager tokenManager)
        { 
            _testContract = testContract;
            _tokenManager = tokenManager;
        } 
        /// <summary>
        /// 测试demo
        /// </summary>
        /// <returns></returns> 
        [HttpGet]
        public async Task<IActionResult> GetDemo()
        {
            var list = _testContract.GetList();
            await Task.Delay(1000);
            return Ok(list);
        }
        /// <summary>
        /// 测试demo
        /// </summary>
        /// <returns></returns> 
        [HttpGet]
        public async Task<IActionResult> GetToken()
        {
            var token = _tokenManager.Token;
            Console.WriteLine($"TestController 主线程Id === {Thread.CurrentThread.ManagedThreadId}");


            var dt=DateTime.Now;

            var tsp=  TimeStampHelper.GetTimeStamp(dt);
             
            var dtnow = TimeStampHelper.GetDateTime(tsp.ToString());
            
            await Task.Delay(1000);

            return Ok(token);
        }
        /// <summary>
        /// 测试demo
        /// </summary>
        /// <returns></returns> 
        [HttpGet] 
        public async Task<IActionResult> Get()
        {
            //var aaa = await _gitHubClient.GetData();

            var CurrentID = CacheFactory.Cache.GetCache<string>("CurrentID");

            if (string.IsNullOrEmpty(CurrentID))
            {
                CurrentID = DateTime.Now.Year.ToString();
                CacheFactory.Cache.SetCache("CurrentID", CurrentID);
            }
            await Task.Delay(100);
            return Ok(new { CurrentID });
        }
         
        /// <summary>
        /// 测试demo
        /// </summary>
        /// <returns></returns>
        [HttpPost("PostTest")]
        [PreventDoublePost] 
        public IActionResult PostTest(int Id)
        {  
            return Ok(new { Id });
        }




        private IActionResult Upload([FromForm] IFormCollection formCollection)
        {
            var files = formCollection.Files;
            long size = files.Sum(f => f.Length);
            var Keys = formCollection.Keys;
            string contentRootPath = AppContext.BaseDirectory;
            List<string> filenames = new List<string>();
            foreach (IFormFile formFile in files)
            {
                if (formFile.Length > 0)
                {
                    string fileExt = Path.GetExtension(formFile.FileName);
                    long fileSize = formFile.Length;
                    string newFileName = Guid.NewGuid().ToString() + fileExt;
                    var filePath = contentRootPath + "/fileUpload/";
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    using (var stream = new FileStream(filePath + newFileName, FileMode.Create))
                    {
                        formFile.CopyTo(stream);
                    }
                    filenames.Add(filePath + newFileName);
                }
            }
            return Ok(new { message = "上传成功", code = 200, data = filenames });
        }


        private TData SaveFile(string savePath, IFormFileCollection files)
        { 
            List<string> filenames = new List<string>();
            try
            {
                foreach (IFormFile formFile in files)
                {
                    if (formFile.Length > 0)
                    {
                        string fileExt = Path.GetExtension(formFile.FileName);
                        long fileSize = formFile.Length;
                        string newFileName = Guid.NewGuid().ToString("N") + fileExt;
                        if (!Directory.Exists(savePath))
                        {
                            Directory.CreateDirectory(savePath);
                        }
                        using (var stream = new FileStream(savePath + newFileName, FileMode.Create))
                        {
                            formFile.CopyTo(stream);
                        }
                        filenames.Add(savePath + newFileName);
                    }
                }
                return new TData(ResultTag.success, "上传成功", filenames);
            }
            catch (Exception)
            {
                return new TData(ResultTag.fail, "上传失败", filenames);
            } 
        }

    }
}
